package com.shycloud.mido.member.controller;

import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.MultiClassroom;
import com.shycloud.mido.member.api.entity.MultiTeacherClassroomLink;
import com.shycloud.mido.member.api.vo.MyTeacherVO;
import com.shycloud.mido.member.service.MultiClassroomService;
import com.shycloud.mido.member.service.MultiTeacherClassroomLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 教室添加教师
 *
 * @author baiqiang
 * @since 2021-07-12
 */
@RestController
@AllArgsConstructor
@Api(tags = "教室操作教师")
@RequestMapping("/v1/multi-teacher-classroom-link")
public class MultiTeacherClassroomLinkController {

	/** 教师教室模块 service */
	private final MultiTeacherClassroomLinkService teacherClassroomService;
	/** 教室模块 service */
	private final MultiClassroomService multiClassroomService;

	@ApiOperation(value = "后台管理-教室添加教师", notes = "所需要传递参数:teacherId,classroomId,orgId")
	@PostMapping("addTeacher")
	public R<Boolean> addTeacher(@RequestBody MultiTeacherClassroomLink teacherClassroomLink) {
		return new R<Boolean>(teacherClassroomService.addTeacher(teacherClassroomLink));
	}

	@ApiOperation("后台管理-移除教师")
	@PostMapping("delTeacher")
	public R delTeacher(@RequestBody Map<String, Object> reqMap) {

		List<String> teacherIds = (List<String>) reqMap.get("teacherIds");

		String classroomId = (String) reqMap.get("classroomId");

		for (String teacherId : teacherIds) {
			teacherClassroomService.delTeacher(teacherId, classroomId);
		}
		return new R(true);
	}

	@ApiOperation("后台管理-获取教师列表")
	@GetMapping("getTeacherListByRoomId")
	public R getTeacherListByRoomId(String classroomId) {
		return new R(teacherClassroomService.getTeacherListByRoomId(classroomId));
	}

	@ApiOperation("APP-教师通讯录")
	@GetMapping("getTeacherList")
	public R getTeacherList(String studentId, String teacherName) {
		return new R(teacherClassroomService.getTeacherList(studentId, teacherName));
	}

	@ApiOperation("APP-教师列表")
	@GetMapping("getTeacherListFast")
	public R getTeacherListFast(String studentId, String classroomId) {

		if (StringUtils.isEmpty(classroomId)) {
			return new R(teacherClassroomService.getTeacherList(studentId));
		} else {
			return new R(teacherClassroomService.getTeacherListByRoomId(classroomId));
		}

	}

	@ApiOperation(value = "APP-添加助教/教师", notes = "所需要传递参数:username,classroomId")
	@PostMapping("addTeacherApp")
	public R<Boolean> addTeacherApp(@RequestBody Map<String, String> reqMap) {

		String classroomId = reqMap.get("classroomId");
		//获取添加教师的账号
		String teaUsername = reqMap.get("username");
		//获取当前教师ID
		String memberId = SecurityUtils.getMember().getId();

		MultiClassroom classroom = multiClassroomService.getById(classroomId);

		if (classroom != null && !memberId.equals(classroom.getCreateUser())) {
			return new R<Boolean>(CommonConstants.FAIL, "此教师不是教室创建者,无法添加教师", false);
		}

		return new R<Boolean>(teacherClassroomService.addTeacherApp(teaUsername, classroom));
	}

	@ApiIgnore
	@ApiOperation("feign-获取教师列表")
	@GetMapping("getTeacherListByRoomIdFeign")
	public List<MyTeacherVO> getTeacherListByRoomIdFeign(String classroomId) {
		return teacherClassroomService.getTeacherListByRoomId(classroomId);
	}

}
